
#include "StdAfx.h"
#include "TurnNumber.h"

TurnNumber::TurnNumber(){
	number_ = 0;
	max_number_ = 0;
}

TurnNumber::TurnNumber(unsigned __int64 max_number){
	reset( max_number );
}

TurnNumber::~TurnNumber(){
}

void TurnNumber::setMaxValue( unsigned __int64 max_number ){
	max_number_ = max_number;
}

unsigned __int64 TurnNumber::isTurn(){
	unsigned __int64 value = 0;

	if( number_ < max_number_ ){
		value = number_;
		number_++;
		return value;
	}

	number_ = 0;
	return 0;
}

void TurnNumber::reset( unsigned __int64 max_number ){
	number_ = 0;
	max_number_ = max_number;
}

void TurnNumber::reset(){
	number_ = 0;
}

bool TurnNumber::isValuePrevIntervalSafe( unsigned __int64 value, unsigned __int64 interval ){
	unsigned __int64 prev = 0;	

	if( number_ < interval ){
		if( ( value < ( max_number_ - interval ) ) &&
			( value > number_ ) )
		{
			return false;
		}

		unsigned __int64 i = interval - number_;
		prev = max_number_ - i;

		if( ( prev > value ) && ( max_number_ >= value ) )
			return true;

		if( ( 0 < value ) && ( number_ <= value ) )
			return true;
	}
	else{
		prev = number_ - interval;

		if( value > number_ )
			return false;

		if( prev <= value )
			return true;
	}

	return false;
}
